<?php
 /**
 * 功能描述：	访客统计
 * @datetime	2023-12-11
 * @version   	v1.0
 * @author   	ZHL
 * @copyrigh  	2023 ZHL 版权所有
 */
class C_ClientController extends Controller {

    /**
     * Notes:获取访客信息
     * User: ZhuHaili
     * Date: 2023/12/11
     */
    function info() {
        echo 'var clientInfo = {"ip": "'.clientIP().'"};';
    }

    /**
     * Notes:保存统计信息
     * User: ZhuHaili
     * Date: 2023/12/12
     */
    function save(){
        header("Access-Control-Allow-Origin: *");
        $flag = Fun::input('zhlC_action','');
        $still = Fun::input(SYS_URL_FIX_STILL);

        $param['ip'] = Fun::input('zhlC_IP',clientIP());
        $param['new_user'] = Fun::input('zhlC_new_user','N');
        $param['date_start'] = Fun::input('zhlC_date_start');
        $param['date_end']= Fun::input('zhlC_date_end','');
        $param['page_url'] = Fun::input('zhlC_page_url','');
        $param['page_title'] = Fun::input('zhlC_page_title','');
        $param['page_rate'] = Fun::input('zhlC_page_rate',0);
        $param['from_app'] = Fun::input('zhlC_from_app','');
        $param['from_url'] = Fun::input('zhlC_from_url','');
        $param['from_os'] = Fun::input('zhlC_from_os','');
        $param['table'] = Fun::input('zhlC_table','');
        $param['id'] = Fun::input('zhlC_id',0);
        $result = array('code'=>'40000','msg'=>'Fail');
        if($GLOBALS['ADMIN']['counts_flag'] != 1){
            $result = array('code'=>'40002', 'msg'=>'Counts close');
            Fun::json($result,true);
        }
        if($still != 1){
            $result = array('code'=>'40003', 'msg'=>'unlawful');
            Fun::json($result,true);
        }
        $ser = new C_ClientServe();
        if($flag == 'add'){
            //$param = json_decode('{"ip":"127.0.0.1","new_user":"N","date_start":"2023-12-13 14:31:17","date_end":"","page_url":"http://127.0.0.1/w/frame/zhlcms2.3/","page_title":"ZHLCMS管理系统","page_rate":"47.93","from_app":"Chrome","from_url":"http://127.0.0.1/w/frame/zhlcms2.3/","from_os":"Windows","table":"","id":0}',true);
            $result = $ser->addCountInfo($param);
        }else if($flag == 'update'){
            $result = $ser->updateCountInfo($param);
        }
        Fun::json($result,true);
    }

    /**
     * Notes:获取时间段统计
     * User: ZhuHaili
     * Date: 2023/12/14
     */
    public function stat(){
        $dateStart = Fun::input('date_start','');
        $dateEnd = Fun::input('date_end','');
        $ser = new C_ClientServe();
        $result = $ser->getDayStat($dateStart, $dateEnd);
        Fun::json($result,true);
    }

    /**
     * Notes:获取最近几日走势图
     * User: ZhuHaili
     * Date: 2023/12/14
     */
    public function trend(){
        $day = Fun::input('limit', 7, 'd');
        $dateStart = Fun::input('date_start', date("Y-m-d H:i:s", strtotime("-$day day")));
        $dateEnd = Fun::input('date_end', date('Y-m-d H:i:s'));
        $ser = new C_ClientServe();
        $result = $ser->getRecentTrend($dateStart, $dateEnd);
        Fun::json($result,true);
    }

    /**
     * Notes:获取最近30天应用比例
     * User: ZhuHaili
     * Date: 2023/12/14
     */
    function appratio(){
        $day = Fun::input('limit', 30, 'd');
        $dateStart = Fun::input('date_start', date("Y-m-d H:i:s", strtotime("-$day day")));
        $dateEnd = Fun::input('date_end', date('Y-m-d H:i:s'));
        $ser = new C_ClientServe();
        $result = $ser->getAppRatio($dateStart, $dateEnd);
        Fun::json($result,true);
    }

    /**
     * Notes:今日访客详情
     * User: ZhuHaili
     * Date: 2023/12/14
     */
    public function dayinfo(){
        $nowDate = date("Y-m-d");
        $dateStart = Fun::input('date_start', $nowDate);
        $dateEnd = Fun::input('date_end', $nowDate);
        $countFlag = Fun::input('flag');
        $ser = new C_ClientServe();
        $result = $ser->getDayCountInfos($dateStart, $dateEnd, $countFlag);
        Fun::json($result,true);
    }

    /**
     * Notes:修改访客地区
     * User: ZhuHaili
     * Date: 2023/2/21
     */
    function updatearea(){
        $id = Fun::input('id');
        $area = Fun::input('name');
        $area = Fun::trimStr($area);
        $area = trimAll($area);
        $ser = new C_ClientServe();
        $result = $ser->updateArea($id, $area);
        Fun::json($result,true);
    }

    /**
     * Notes:根据IP获取页面访问排行榜
     * User: ZhuHaili
     * Date: 2023/12/14
     */
    function pageips(){
        $limit = Fun::input('limit',0,'d'); // 前十
        $nowDate = date("Y-m-d");
        $dateStart = Fun::input('date_start', $nowDate);
        $dateEnd = Fun::input('date_end', $nowDate);
        $ser = new C_ClientServe();
        $result = $ser->getPageIpsCounts($dateStart, $dateEnd, $limit);
        Fun::json($result,true);
    }

    /**
     * Notes:页面访问排行榜详情
     * User: ZhuHaili
     * Date: 2023/12/14
     */
    function pageipsinfo(){
        $nowDate = date("Y-m-d");
        $dateStart = Fun::input('date_start', $nowDate);
        $dateEnd = Fun::input('date_end', $nowDate);
        $pageUrl = Fun::input('page_url');
        $ser = new C_ClientServe();
        $result = $ser->getPageIpsCountInfo($dateStart, $dateEnd, $pageUrl);
        Fun::json($result,true);
    }

}